//
//  ComboxView.m
//  MtProject
//
//  Created by xp on 17/5/4.
//  Copyright © 2017年 Theia. All rights reserved.
//

#import "ComboxView.h"

@implementation ComboxView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];

    }
    return self;
}

-(void)initSubViews{
    UITableView *tbv_main = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    tbv_main.delegate = self;
    tbv_main.dataSource = self;
    tbv_main.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tbv_main.bounces = false;
    [self addSubview:tbv_main];
    self.tbv_main = tbv_main;
    
    //@[@"r_good_03",@"点赞"],@[@"r_report",@"举报"]
    //[self setArr_main:@[@"待回答",@"已回答",@"已过期"].mutableCopy];
}

//-(void)setArr_main:(NSMutableArray *)arr_main{
//    _arr_main = arr_main;
//    self.tbv_main.height = self.arr_main.count * ROW_HEIGHT;
//    self.height = self.arr_main.count * ROW_HEIGHT;
//    [self.tbv_main reloadData];
//}
-(void)setTextArr:(NSArray *)textArr{
    _textArr=textArr;
    self.tbv_main.height = _textArr.count * ROW_HEIGHT;
    self.height = _textArr.count * ROW_HEIGHT;
    [self.tbv_main reloadData];
}
-(void)setIconURLArr:(NSArray *)iconURLArr {
    _iconURLArr = iconURLArr;
    [self.tbv_main reloadData];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    if ([self.tbv_main respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tbv_main setSeparatorInset:UIEdgeInsetsMake(0, CELL_LEFT_MARGIN, 0, CELL_LEFT_MARGIN)];
    }
    if ([self.tbv_main respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tbv_main setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _textArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCells"];
    if (!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuCells"];
        cell.selectedBackgroundView=[[UIView alloc]initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor=GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY);
        //cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UIImageView *imgv_icon = [[UIImageView alloc]init];
        [cell.contentView addSubview:imgv_icon];
        imgv_icon.tag=100;
        imgv_icon.contentMode = UIViewContentModeScaleAspectFit;
        imgv_icon.sd_layout.leftSpaceToView(cell.contentView,15).centerYEqualToView(cell.contentView).widthIs(25).heightEqualToWidth();
        
        UILabel *lab_title = [[UILabel alloc]init];
        [cell.contentView addSubview:lab_title];
        lab_title.tag=101;
        lab_title.font = GET_SYSTEM_FONT(TEXT_FONT_30);
        lab_title.textColor = GET_HEX_COLOR(TEXT_GRAY);
        lab_title.sd_layout.leftSpaceToView(imgv_icon,10).centerYEqualToView(cell.contentView).rightSpaceToView(cell.contentView,10).heightIs(20);
    }
    
     UIImageView *imgv_icon = [cell.contentView viewWithTag:100];
     UILabel *lab_title = [cell.contentView viewWithTag:101];
//    cell.imageView.sd_layout.widthIs(15).heightIs(15).centerYEqualToView(cell.contentView);
    [imgv_icon sd_setImageWithURL:[NSURL URLWithString:_iconURLArr[indexPath.row]]];
    //cell.textLabel.sd_layout.centerXEqualToView(cell.textLabel.superview);
    //cell.textLabel.font = GET_SYSTEM_FONT(TEXT_FONT_42);
    
    
        lab_title.text = _textArr[indexPath.row];
    
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ROW_HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.clickBlock) {
        self.clickBlock(indexPath.row,self);
    }
}


@end
